#ifndef QMICROPHONE_H
#define QMICROPHONE_H

#include <QObject>
#include <QFile>
#include <QAudioInput>
#include <QAudioOutput>

#include "qvolumeanalyzer.h"

class QMicrophone : public QObject
{
    Q_OBJECT
    Q_CLASSINFO("Author", "Sebastiano Galazzo")
    Q_CLASSINFO("Email", "sebastiano.galazzo@gmail.com")
    Q_PROPERTY(int threshold READ threshold WRITE setThreshold)

public:

    static const int DataFrequencyHzInput;

    explicit QMicrophone(QObject *parent = 0);
    
    Q_INVOKABLE QStringList supportedCodecs();
    Q_INVOKABLE QString statusString();
    Q_INVOKABLE bool isRecording();

    int threshold();
    void setThreshold(int value);

signals:
    void recordingStarted();
    void recordingStopped();

    void volumeAnalysisStarted();
    void volumeAnalysisStopped();

    void stateChanged(QAudio::State);
    void thresholdExceeded(int volume);
    void clap();

public slots:
    /**
    *   @brief start recording
    *   @param name where file recorded is stored
    *   @param time recording in milliseconds
    */
    void startRecording(QString name, int time=-1);

    /**
    *   @brief stop recording
    */
    void stopRecording();

    void startVolumeAnalysis(int time=-1);
    void stopVolumeAnalysis();

    void microphoneStateChanged(QAudio::State);
    void volumeManagerStateChanged(QAudio::State);

    void play(QString name);

private slots:
    void volumeThresholdExceeded(int);
    void clapEvent();

private:
    QFile outputFile;
    QFile inputFile;
    QAudioFormat format;
    QAudioInput* audio;
    QAudioInput* volumeManager;
    QAudioOutput *m_audioOutput;
    QVolumeAnalyzer* volumeAnalyzer;
    bool recording;
};

#endif // QMICROPHONE_H
